import { getUuid } from "@/utils/common.methods";
import { UserInfo } from "./user";

export type NoteInfo = {
  uid: string;
  remark: string;
  bgcolor?: string;
  user?: UserInfo;
  thumbup?: number;
  thumbtype?: ThumbType;
  share?: number;
  purview: PurviewType;
}

export enum ThumbType {
  Default,
  Up,
  Down,
}

/** 权限 */
export enum PurviewType {
  Public = 0,
  Private = 1
}

export type ShowNoteInfo = {
  uid?: string;
  left: string, top: string, bgColor: string, note: NoteInfo
}

export class NoteModel {
  private _total: number = 0;
  private _list: NoteInfo[] = [];
  private _colors = ["#cdb4db", "#ffc8dd", "#ffafcc", "#bde0fe", "#a2d2ff"];

  public setList(list: NoteInfo[], total: number): void {
    this._list = list;
    this._total = total;
  }
  public addNote(info: NoteInfo): void {
    this._list = (this._list || []).concat(info);
    this._total++;
  }
  public deleteNote(uid: string): void {
    this._list = this._list.filter(val => val.uid !== uid);
    this._total--;
  }

  public get list(): NoteInfo[] {
    return this._list;
  }
  public get total(): number {
    return this._total;
  }

  /** 获取随机留言方块 */
  public getRandomNote(clientWidth: number, clientHeight: number,): ShowNoteInfo {
    const uid = getUuid();
    const left = (Math.random() * clientWidth - 100) + "px";
    const top = (Math.random() * clientHeight) + "px";
    const bgColor = this.colorRandom;
    const note = this._list[Math.floor(Math.random() * this._list.length)];
    return { uid, left, top, bgColor, note };
  }

  public get colors(): string[] {
    return this._colors
  }

  public get colorRandom(): string {
    return this._colors[Math.floor(Math.random() * this._colors.length)]
  }
}